<?php
namespace srvs;
use myVendor\PageService;

class ContentService extends  BaseService {
    public static function getModelName(){
        return "Content";
    }

    public function get_data_for_show($menu,$page,$page_size){
        $menu_id=$menu["objectId"];
        $js_function_name="goToPage";
        $result=["content_list"=>[],"page_html"=>""];
        $ContentDetailService=new ContentDetailService();
        $skip=0;
        if(!empty($page)&&$page>1){
            $skip=($page-1)*$page_size;
        }
        $content_list=$this->getAll(["menu_id"=>$menu_id,"status"=>0],["is_top desc","createdAt desc"],$page_size,$skip);
        if(empty($content_list)){
            return $result;
        }

        $MenuService=new MenuService();
        $menu_type=$menu["menu_type"];
        if(!empty($menu["parent_id"])){
            $parent_menu=$MenuService->getOne(["objectId"=>$menu["parent_id"]]);
            $menu_type=$parent_menu["menu_type"];
        }

        if($menu_type==2){
            $content=current($content_list);
            $content_detail=$ContentDetailService->getOne(["content_id"=>$content["objectId"]]);
            $content["detail"]=$content_detail["detail"];
            $result["content_list"]=[$content];
            $this->update(["objectId"=>$content["objectId"],"hits"=>(int)$content["hits"]+1]);
            return $result;
        }
        else{
            $total_count=$this->getCount(["menu_id"=>$menu_id,"status"=>0]);
            $PageService=new PageService($total_count,$page_size,$page,$js_function_name);
            $page_html=$PageService->getPageHTML();
            $result["content_list"]=$content_list;
            $result["page_html"]=$page_html;
            return $result;
        }
    }
}